////import org.openqa.selenium.Cookie;
////import org.openqa.selenium.WebDriver;
////
////import java.io.*;
////import java.util.Set;
////
////public class CookieUtils {
////
////    public static void saveCookies(WebDriver driver, String file) throws IOException {
////        driver.get("https://www.zhipin.com");
////        Set<Cookie> cookies = driver.manage().getCookies();
////        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
////            oos.writeObject(cookies);
////        }
////    }
////
////    @SuppressWarnings("unchecked")
////    public static Set<Cookie> loadCookies(String filePath) throws IOException, ClassNotFoundException {
////        return loadCookies(new File(filePath));
////    }
////
////    @SuppressWarnings("unchecked")
////    public static Set<Cookie> loadCookies(File file) throws IOException, ClassNotFoundException {
////        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
////            return (Set<Cookie>) ois.readObject();
////        }
////    }
////}
////
////
//import org.openqa.selenium.Cookie;
//import org.openqa.selenium.WebDriver;
//
//import java.io.*;
//import java.util.Set;
//
//public class CookieUtils {
//
//    /**
//     * 保存当前页面的 Cookie 到文件
//     */
//    public static void saveCookies(WebDriver driver, String file) throws IOException {
//        // 确保在目标页面后再保存 Cookie
//        if (!driver.getCurrentUrl().contains("zhipin.com")) {
//            throw new IOException("当前不在 boss.zhipin.com 页面");
//        }
//
//        Set<Cookie> cookies = driver.manage().getCookies();
//        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
//            oos.writeObject(cookies);
//        }
//        System.out.println("✅ Cookie 已保存至: " + file);
//    }
//
//    /**
//     * 从文件加载 Cookie
//     */
//    @SuppressWarnings("unchecked")
//    public static Set<Cookie> loadCookies(String filePath) throws IOException, ClassNotFoundException {
//        return loadCookies(new File(filePath));
//    }
//
//    @SuppressWarnings("unchecked")
//    public static Set<Cookie> loadCookies(File file) throws IOException, ClassNotFoundException {
//        if (!file.exists()) {
//            throw new FileNotFoundException("Cookie 文件不存在: " + file.getAbsolutePath());
//        }
//
//        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
//            Set<Cookie> cookies = (Set<Cookie>) ois.readObject();
//            System.out.println("✅ 成功加载 " + cookies.size() + " 个 Cookie");
//            for (Cookie c : cookies) {
//                System.out.println(" - " + c.getName() + " = " + c.getValue());
//            }
//            return cookies;
//        }
//    }
//}
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;

import java.io.*;
import java.util.Set;

public class CookieUtils {

    /**
     * 保存当前页面的 Cookie 到文件
     */
    public static void saveCookies(WebDriver driver, String file) throws IOException {
        // 确保在目标页面后再保存 Cookie
        if (!driver.getCurrentUrl().contains("zhipin.com")) {
            throw new IOException("当前不在 boss.zhipin.com 页面");
        }

        Set<Cookie> cookies = driver.manage().getCookies();
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
            oos.writeObject(cookies);
        }
        System.out.println("✅ Cookie 已保存至: " + file);
    }

    /**
     * 从文件加载 Cookie
     */
    @SuppressWarnings("unchecked")
    public static Set<Cookie> loadCookies(String filePath) throws IOException, ClassNotFoundException {
        return loadCookies(new File(filePath));
    }

    @SuppressWarnings("unchecked")
    public static Set<Cookie> loadCookies(File file) throws IOException, ClassNotFoundException {
        if (!file.exists()) {
            throw new FileNotFoundException("Cookie 文件不存在: " + file.getAbsolutePath());
        }

        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
            Set<Cookie> cookies = (Set<Cookie>) ois.readObject();
            System.out.println("✅ 成功加载 " + cookies.size() + " 个 Cookie");
            for (Cookie c : cookies) {
                System.out.println(" - " + c.getName() + " = " + c.getValue());
            }
            return cookies;
        }
    }
}
